package java_learn.effective_java.createAndDestroy_2;

/**
 * @Author: 李浩然
 * @Date: 2021/1/25 11:24 上午
 */
public class NutritionFacts_builder {
    private final int servingSize;
    private final int servings;
    private final int calories;
    private final int fat;
    private final int sodium;
    private final int carbohydrate;

    public static class Builder {
        //必须的参数
        private final int servingSize;
        private final int servings;
        //可选参数
        private int calories = 0;
        private int fat = 0;
        private int sodium = 0;
        private int carbohydrate = 0;

        public Builder(int servingSize, int servings) {
            this.servingSize = servingSize;
            this.servings = servings;
        }

        public Builder calories(int val) {
            calories = val;
            return this;
        }

        public Builder fat(int val) {
            fat = val;
            return this;
        }

        public Builder sodium(int val) {
            sodium = val;
            return this;
        }

        public Builder carbohydrate(int val) {
            carbohydrate = val;
            return this;
        }

        public NutritionFacts_builder build() {
            return new NutritionFacts_builder(this);
        }
    }

    private NutritionFacts_builder(Builder builder) {
        servings = builder.servings;
        servingSize = builder.servingSize;
        calories = builder.calories;
        fat = builder.fat;
        sodium = builder.sodium;
        carbohydrate = builder.carbohydrate;
    }

    public static void main(String[] args) {
        NutritionFacts_builder cocaCola = new Builder(240, 8).calories(100).sodium(35).carbohydrate(27).build();
    }
}
